import aiohttp

from app_v2.bili_api.common import headers_


class VideoInfo:
    def __init__(self, view, tags: list):
        self.data = view
        self.bvid = self.data["bvid"]
        self.view = self.data["stat"]["view"]
        self.danmaku = self.data["stat"]["danmaku"]
        self.reply = self.data["stat"]["reply"]
        self.like = self.data["stat"]["like"]
        self.coin = self.data["stat"]["coin"]
        self.favorite = self.data["stat"]["favorite"]
        self.share = self.data["stat"]["share"]
        self.pubdate = self.data["pubdate"]
        self.mid = self.data["owner"]["mid"]
        self.cid = self.data["cid"]
        self.pic = self.data["pic"]
        self.desc = self.data["desc"]
        self.title = self.data["title"]
        self.tid = self.data["tid"]
        self.tname = self.data["tname"]
        self.duration = self.data["duration"]
        self._tags = list(map(lambda item: item["tag_name"], tags))
        # here to add

    def to_dict(self):
        res = {}
        for k, v in self.__dict__.items():
            if k != 'data' and not k.startswith('_'):
                res[k] = v
        return res

    async def tags(self):
        if not self._tags or len(self._tags) == 0:
            url = f"https://api.bilibili.com/x/web-interface/view/detail/tag?bvid={self.bvid}"
            async with aiohttp.ClientSession() as session:
                async with session.get(url, headers=headers_) as resp:
                    resp = await resp.json()
            self._tags = list(map(lambda item: item["tag_name"], resp["data"]))
        return self._tags


class UserInfo:
    def __init__(self, mid, name, followers, likes, videos, views, following, pic=None):
        self.mid = mid
        self.name = name
        self.followers = followers
        self.likes = likes
        self.videos = videos
        self.views = views
        self.following = following
        self.pic = pic

    def to_dict(self):
        return self.__dict__
